/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DataAccess.DataAccessFacade;
import DomainModel.Grade;
import DomainModel.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mershack
 */
@WebServlet(name = "ViewClassGradesController", urlPatterns = {"/ViewClassGrades"})
public class ViewClassWorkGradesController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");

        if (user.getRole() != Role.PROF) {
            throw new ServletException("Forbidden");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        try {
            int sectionId = Integer.parseInt(request.getParameter("sectionId"));
            int classWorkId = Integer.parseInt(request.getParameter("classWorkId"));
            DataAccessFacade dataAccessFacade = DataAccessFacade.getInstance();

            List<StudentAndGrade> studentAndGrades = dataAccessFacade.getSectionGrades(sectionId, classWorkId);
            Section section = dataAccessFacade.getSectionById(sectionId);
            ClassWork classwork = dataAccessFacade.getClassWorkById(classWorkId);
            request.setAttribute("studentAndGrades", studentAndGrades);
            request.setAttribute("section", section);
            request.setAttribute("classWork", classwork);
            request.getRequestDispatcher("/WEB-INF/grs/enterClassWorkGrades.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet requestadminp
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);//call get Again

        //processRequest(request, response);

        //get the student ids
        /*int studentIds[] = convertStringArrayToIntArray(request.getParameterValues("studentId"));
         //get the points earned
         int pointsEarned[] = convertStringArrayToIntArray(request.getParameterValues("pointsEarned"));

         //commentGiven
         String comments[] = request.getParameterValues("comment");

         //save in the database, pass the section id, classwork id, and all students, pointsEarned and comments array
         //this save will be insert

         for (int i = 0; i < studentIds.length; i++) {
         System.out.println("Student id: " + studentIds[i] + ", point: " + pointsEarned[i] + ", comment: " + comments[i]);
         }*/
    }

    private int[] convertStringArrayToIntArray(String[] strArray) {
        int intArray[] = new int[strArray.length];

        for (int i = 0; i < strArray.length; i++) {
            if (strArray[i].isEmpty()) {
                intArray[i] = 0;
            } else {
                intArray[i] = Integer.parseInt(strArray[i]);
            }

        }


        return intArray;


    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
